#include "mainwidget.h"
#include "./ui_mainwidget.h"

MainWidget* MainWidget::instance = nullptr;

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    this->setWindowTitle("BlueChat");
    this->setWindowIcon(QIcon("://resource/image/wechat.png"));
    initUI();
}

MainWidget* MainWidget::getInstance()
{
    if(instance == nullptr)
    {
        instance = new MainWidget;
    }
    return instance;
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::initUI()
{
    // 1. 初始化整体窗⼝布局
    initMainWindow();
    // 2. 初始化左侧
    initWindowLeft();
    // 3. 初始化中间
    initWindowMid();
    // 4. 初始化右侧
    initWindowRight();
}

void MainWidget::initMainWindow()
{
    QHBoxLayout* layout = new QHBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0); //控件内容与控件边界之间的间距
    this->setLayout(layout);
    windowLeft = new QWidget();
    windowMid = new QWidget();
    windowRight = new QWidget();
    windowLeft->setObjectName("windowLeft");
    windowMid->setObjectName("windowMid");
    windowRight->setObjectName("windowRight");
    windowLeft->setFixedWidth(70);
    windowMid->setFixedWidth(310);
    // windowRight 宽度⾃适应, 不指定固定值, 只指定最⼩值
    windowRight->setMinimumWidth(700);
    layout->addWidget(windowLeft);
    layout->addWidget(windowMid);
    layout->addWidget(windowRight);
    QString style = "#windowLeft { background-color: rgb(46, 46, 46); }"
                    "#windowMid { background-color: rgb(247, 247, 247); } "
                    "#windowRight {background-color: rgb(245, 245, 245);} ";
    this->setStyleSheet(style);

}

void MainWidget::initWindowLeft()
{
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(0, 50, 0, 0);
    layout->setSpacing(20);
    windowLeft->setLayout(layout);

    // 初始化⽤⼾头像
    userAvatar = new QPushButton();
    userAvatar->setFixedSize(45, 45);
    userAvatar->setIconSize(QSize(45, 45));

//#if TEST_UI
    userAvatar->setIcon(QIcon("://resource/image/defaultAvatar.jpg"));
//#endif
    layout->addWidget(userAvatar, 1, Qt::AlignTop | Qt::AlignHCenter);

// 初始化 tab 按钮
    sessionTabBtn = new QPushButton();
    sessionTabBtn->setFixedSize(45, 45);
    sessionTabBtn->setIcon(QIcon("://resource/image/session_active.png"));
    sessionTabBtn->setIconSize(QSize(30, 30));
    sessionTabBtn->setStyleSheet("QPushButton { background-color: transparent;}");
    layout->addWidget(sessionTabBtn, 1, Qt::AlignTop | Qt::AlignHCenter);

    friendTabBtn = new QPushButton();
    friendTabBtn->setFixedSize(45, 45);
    friendTabBtn->setIcon(QIcon("://resource/image/friend_inactive.png"));
    friendTabBtn->setIconSize(QSize(30, 30));
    friendTabBtn->setStyleSheet("QPushButton { background-color: transparent;}");
    layout->addWidget(friendTabBtn, 1, Qt::AlignTop | Qt::AlignHCenter);

    applyTabBtn = new QPushButton();
    applyTabBtn->setFixedSize(45, 45);
    applyTabBtn->setIcon(QIcon("://resource/image/apply_inactive.png"));
    applyTabBtn->setIconSize(QSize(30, 30));
    applyTabBtn->setStyleSheet("QPushButton { background-color: transparent;}");

    layout->addWidget(applyTabBtn, 1, Qt::AlignTop | Qt::AlignHCenter);
    // 下⽅添加空隙, 把上述控件挤上去.
    layout->addStretch(20);

    // 处理 tab 按钮的点击操作
    connect(sessionTabBtn, &QPushButton::clicked, this,
            &MainWidget::switchTabToSession);
    connect(friendTabBtn, &QPushButton::clicked, this,
            &MainWidget::switchTabToFriend);
    connect(applyTabBtn, &QPushButton::clicked, this,
            &MainWidget::switchTabToApply);
    ////////////////////////////////////////////
    /// 点击⽤⼾⾃⼰头像, 显⽰出⽤⼾详细信息
    ////////////////////////////////////////////
    connect(userAvatar, &QPushButton::clicked, this, [ = ]()
    {
        SelfInfoWidget* selfInfoWidget = new SelfInfoWidget();
        // 按照模态⽅式弹出对话框
        selfInfoWidget->exec();
    });
}

void MainWidget::switchTabToSession()
{
    // 1. 设置 activeTab
    activeTab = SESSION_LIST;
    // 2. 调整图标⾼亮

    sessionTabBtn->setIcon(QIcon("://resource/image/session_active.png"));
    friendTabBtn->setIcon(QIcon("://resource/image/friend_inactive.png"));
    applyTabBtn->setIcon(QIcon("://resource/image/apply_inactive.png"));
    // 3. TODO 加载会话列表数据
    //this->loadSessionList();
}
void MainWidget::switchTabToFriend()
{
    // 1. 设置 activeTab
    activeTab = FRIEND_LIST;
    // 2. 调整图标⾼亮
    sessionTabBtn->setIcon(QIcon("://resource/image/session_inactive.png"));
    friendTabBtn->setIcon(QIcon("://resource/image/friend_active.png"));
    applyTabBtn->setIcon(QIcon("://resource/image/apply_inactive.png"));
    // 3. TODO 加载好友列表数据
    //this->loadFriendList();
}

void MainWidget::switchTabToApply()
{
    // 1. 设置 activeTab
    activeTab = APPLY_LIST;
    // 2. 调整图标⾼亮
    sessionTabBtn->setIcon(QIcon("://resource/image/session_inactive.png"));
    friendTabBtn->setIcon(QIcon("://resource/image/friend_inactive.png"));
    applyTabBtn->setIcon(QIcon("://resource/image/apply_active.png"));
    // 3. TODO 加载好友列表数据
    //this->loadApplyList();
}

void MainWidget::initWindowMid()
{
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(0, 20, 0, 0);
    layout->setVerticalSpacing(10);
    layout->setHorizontalSpacing(0);
    windowMid->setLayout(layout);
    // 去除默认边框. 否则边框会对后续布局产⽣影响.
    windowMid->setStyleSheet("QWidget { border: none; }");
    searchEdit = new QLineEdit();
    searchEdit->setFixedHeight(30);
    searchEdit->setPlaceholderText("搜索");
    searchEdit->setStyleSheet("QLineEdit { border-radius: 5px; background-color: rgb(226, 226, 226); padding-left: 5px;}");

    addFriendBtn = new QPushButton();
    addFriendBtn->setFixedSize(30, 30);
    addFriendBtn->setIcon(QIcon("://resource/image/jia.png"));
    QString style = "QPushButton { border-radius: 5px; background-color:rgb(226, 226, 226); } ";
    style += "QPushButton:pressed { background-color: rgb(240, 240, 240); } ";
    addFriendBtn->setStyleSheet(style);
    sessionFriendArea = new SessionFriendArea();
    // // 由于 GridLayout 不能够给不同的⾏设置不同的边距
    // // 因此通过添加空⽩ widget 来控制搜索框和搜索按钮之间的边距
    QWidget* spacer1 = new QWidget();
    spacer1->setFixedWidth(10);
    QWidget* spacer2 = new QWidget();
    spacer2->setFixedWidth(10);
    QWidget* spacer3 = new QWidget();
    spacer3->setFixedWidth(10);

    layout->addWidget(spacer1, 0, 0);
    layout->addWidget(searchEdit, 0, 1);
    layout->addWidget(spacer2, 0, 2);
    layout->addWidget(addFriendBtn, 0, 3);
    layout->addWidget(spacer3, 0, 4);
    // // 参数 1, 0, 1, 5 表⽰放在第 1 ⾏, 第 0 列, ⾏⽅向跨⼀⾏, 列⽅向跨五列.
    layout->addWidget(sessionFriendArea, 1, 0, 1, 5);
    for(int i = 0; i < 3; i++)
    {
        sessionFriendArea->addItem(FriendItemType, "qiqi", QIcon("://resource/image/defaultAvatar.jpg"), "xixi", "want to be");
    }

    // connect(addFriendBtn, &QPushButton::clicked, this, [ = ]()
    // {
    //     ChooseFriendDialog* adig = new ChooseFriendDialog("123");
    //     adig->show();
    // });
}

void MainWidget::initWindowRight()
{
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    windowRight->setLayout(layout);
    /////////////////////////////////////////////////////
    /// 初始化右侧上⽅, 会话标题区
    /////////////////////////////////////////////////////
    QWidget* widget = new QWidget();
    widget->setObjectName("titleWidget");
    widget->setFixedHeight(62);
    widget->setStyleSheet("#titleWidget { border-bottom: 1px solid rgb(231,231, 231); border-left: 1px solid rgb(231, 231, 231); }");
    layout->addWidget(widget, 0, Qt::AlignTop);

    QHBoxLayout* hlayout = new QHBoxLayout();
    hlayout->setContentsMargins(20, 0, 20, 0);
    widget->setLayout(hlayout);
    sessionTitle = new QLabel();
    sessionTitle->setStyleSheet("QLabel { font-size: 22px; }");
#if TEST_UI
    sessionTitle->setText("这⾥是会话标题");
#endif
    hlayout->addWidget(sessionTitle);

    extraButton = new QPushButton();
    extraButton->setIcon(QIcon("://resource/image/more.png"));
    extraButton->setFixedSize(30, 30);
    extraButton->setStyleSheet("QPushButton { background-color: rgb(245, 245,245); border: none;} QPushButton:pressed { background-color: rgb(222, 222,223); }");
    extraButton->setCursor(Qt::PointingHandCursor);
    hlayout->addWidget(extraButton);

    connect(extraButton, &QPushButton::clicked, this, [ = ]()
    {
// 测试阶段的代码
#if TEST_GROUP_SESSION_DETAIL
//         GroupSessionDetailWidget* groupSessionDetailWidget = new
//         GroupSessionDetailWidget();
//         groupSessionDetailWidget->show();
// #else
        ChatSessionInfo tt;
        tt.chatSessionName = "asd";
        tt.avatar = QIcon(":/resource/image/defaultAvatar.jpg");
        tt.chatSessionName = "aaa";
        SessionDetailWidget* sessionDetailWidget = new SessionDetailWidget(tt);
        sessionDetailWidget->show();
#endif
    });


    /////////////////////////////////////////////////////
    /// 初始化右侧中间, 消息显⽰区
    /////////////////////////////////////////////////////
    messageShowArea = new MessageShowArea();
    layout->addWidget(messageShowArea);

#if TEST_UI
    Message* message = new Message();
    message->sender.avatar = QIcon(":/image/defaultAvatar.png");
    message->sender.nickname = "张三";
    message->time = "2024-04-16 17:00";
    message->messageType = MessageType::TEXT_TYPE;
    QString text = "今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥今晚吃啥";
    message->content = text.toUtf8();
    messageShowArea->addMessage(false, *message);
    for (int i = 0; i < 30; i++)
    {
        Message* message = new Message();
        message->sender.avatar = QIcon(":/image/defaultAvatar.png");
        message->sender.nickname = "张三" + QString::number(i);
        message->time = "2024-04-16 17:00";
        message->messageType = MessageType::TEXT_TYPE;
        QString text = "今晚吃啥";
        message->content = text.toUtf8();
        messageShowArea->addMessage(true, *message);
    }
#endif
    /////////////////////////////////////////////////////
    /// 初始化右侧下⽅, 消息编辑区
    /////////////////////////////////////////////////////
    messageEditArea = new MessageEditArea(this);
    layout->addWidget(messageEditArea, 0, Qt::AlignBottom);

}
